// pkgTask/pages/hospital-task-list/index.js
const lbbTaskService = require('../service/task-service.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeTab: 0, // 当前tab
    unFinishList: [],
    finishList: [],
    invalidList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
       let that = this
       that.data.lbmId = options.hospitalId
       this. getTaskList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh({
      success: (res) => {},
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  
  /**
   * 打开任务详情页面
   */
  navTaskDetail(e) {
    let that = this
    const vm = this
    let data = e.currentTarget.dataset
    let tab = data.tab
    let index = data.index
    let lbmName = data.lbmname
    let taskTime = data.tasktime
    let taskStat = data.taskstat
    let hosRecordId = data.hrid
    let task = null
    if (tab == '未完成') {
      task = vm.data.unFinishList[index]
    }else if (tab == '已完成') {
      task = vm.data.finishList[index]
    }else if (tab == '已过期') {
      task = vm.data.invalidList[index]
    }
    console.log("taskList-->", data)
    if (task && task.taskType == 'take-photo') {
      wx.navigateTo({
        url: '/pkgTask/pages/task-takephoto-detail/index?taskId=' + task.taskId + '&lbmId=' + that.data.lbmId + '&lbmName=' + lbmName +
           '&taskTime=' + taskTime + '&taskStat=' + taskStat + '&hosRecordId=' + hosRecordId,
      })
    }
  },

  getTaskList(){
    
    let that = this

    lbbTaskService.getTaskList({     
      lbmId: that.data.lbmId,
    }, function (res){
      console.log(res.data)
      let dataList = res.data
      
      that.data.unFinishList = new Array()
      that.data.finishList = new Array()
      that.data.invalidList = new Array()

      dataList.forEach(v => {
        console.log(v);
        let stat = v.taskStat;
        if(stat == '未完成') {
          that.data.unFinishList.push(v);
        }else if(stat == '已完成') {
          that.data.finishList.push(v);
        }else if(stat == '已过期') {
          that.data.invalidList.push(v);
        }        
      });

      that.setData({
        unFinishList: that.data.unFinishList,
        finishList: that.data.finishList,
        invalidList: that.data.invalidList
      }) 
    })

  },

})